import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter)
// 导入登陆组件
import login from '@/views/login/login.vue'
import layout from '@/views/layout/layout.vue'
import chart from '@/views/chart/chart.vue'
import userlist from '@/views/userlist/userlist.vue'
import question from '@/views/question/question.vue'
import business from '@/views/business/business.vue'
import subject from '@/views/subject/subject.vue'

// 导入vuex
import store from '@/store/index.js';
import { Message } from 'element-ui';
import { removeLocal } from '@/utils/local.js';
// 进度条
import Nprogress from 'nprogress';
import 'nprogress/nprogress.css';

const router = new VueRouter({
    routes: [
        // 重定向
        { path: '/', redirect: '/login' },
        { path: '/login', component: login, meta: { title: '登录', roles: ['超级管理员', '管理员', '老师','学生'] } },
        {
            path: '/layout', component: layout,
            redirect: '/layout/chart',
            children: [
                { path: '/layout/chart', component: chart, meta: { title: '数据概览', roles: ['超级管理员', '管理员', '老师'],icon:'el-icon-pie-chart' } },
                { path: '/layout/userlist', component: userlist, meta: { title: '用户列表', roles: ['超级管理员', '管理员'],icon:'el-icon-user' } },
                { path: '/layout/question', component: question, meta: { title: '题库列表', roles: ['超级管理员', '管理员', '老师'],icon:'el-icon-edit-outline' } },
                { path: '/layout/business', component: business, meta: { title: '企业列表', roles: ['超级管理员', '管理员', '老师'],icon:'el-icon-office-building' } },
                { path: '/layout/subject', component: subject, meta: { title: '学科列表', roles: ['超级管理员', '管理员', '老师', '学生'],icon:'el-icon-notebook-2' } },
            ]
        }

    ]
});

// 路由的导航守卫
//前置导航守卫
router.beforeEach((to, from, next) => {
    Nprogress.start()
    // 判断登录者是否有权限访问
    if (!to.meta.roles.includes(store.state.role)) {
        Message.error('您没有权限访问此页面')
        removeLocal('token')
        next('/login')
    } else {
        next();//通过
    }


});
//后置导航守卫
router.afterEach((to) => {
    document.title = to.meta.title;
    Nprogress.done();
})
export default router;